package com.controller.HdjConroller;

import com.Util.DB;
import com.commont.DiskComment;
import com.commont.HttpUntil;
import com.commont.IpUtil;
import com.controller.BaseController;
import com.exception.JerseyException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.jooq.DSLContext;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import java.sql.Connection;

import static com.Util.UtilBase.*;

/**
 * 查询通用类
 */
@Path ( "query" )
public class CommentConroller extends BaseController {


    /**
     * 查找云盘的数据
     * @param query
     * @return
     */
    @Path ( "disk" )
    @POST
    @Produces(MediaType.APPLICATION_JSON+";charset=utf-8")
    @Consumes(MediaType.APPLICATION_JSON)
    public String getDisk(String query){
        //获取参数
       JsonObject jsonObject=new JsonParser ().parse ( query ).getAsJsonObject ();
       String fromDesc=getJsonAsString ( jsonObject,"fromDesc");
       String current_page=getJsonAsString ( jsonObject,"current_page" );

       //获取数据
        JsonObject resutlt=null;
        try {
            resutlt= DiskComment.getDisk ( fromDesc,current_page );
            return success (resutlt);
        } catch (Exception e) {
            e.printStackTrace ();
            return error ( -1,e.getMessage () );
        }
    }



    /**
     * 查找电影
     * @return
     */
    @Path ("get_movie")
    @POST
    @Produces(MediaType.APPLICATION_JSON+";charset=utf-8")
    @Consumes(MediaType.APPLICATION_JSON)
    public String getMovie(String query){
        JsonObject json=new JsonParser ().parse ( query ).getAsJsonObject ();
        Integer type=getJsonAsInt ( json,"type" );
        Integer movieId=getJsonAsInt ( json,"movieId" );
        Integer page=getJsonAsInt ( json,"page" );
        Integer actorId=getJsonAsInt ( json,"actorId" );
        JsonObject result=new JsonObject ();
        try {
            // 1.获取正在热映的电影 2.马上上映的电影 3.电影详情 4.电影的演员 5.电影的评论 6.电影剧照 7.预告片 8.演员详情
            if (type==1){
                result=HttpUntil.getHotMovie ();
            }else if (type==2){
                result=HttpUntil.getHotMovieAway ();
            }else if (type==3){
                result=HttpUntil.getHotMovieinfo ( movieId );
            }else if (type==4){
                result=HttpUntil.getHotMovieinfoActor ( movieId );
            }else if (type==5){
                result=HttpUntil.getHotMovieinfoComment ( movieId );
            }else if (type==6){
                result=HttpUntil.getHotMovieinfoImg ( movieId );
            }else if (type==7){
                result=HttpUntil.getHotMovieinfoVideo ( movieId,page);
            }else if(type==8){
                result=HttpUntil.getActor ( actorId );
            }
            return success (result);
        } catch (Exception e) {
            return error ( -1,e.getMessage ());
        }

    }

    /**
     * axios_watch测试
     * @param query
     * @return
     */
    @Path ("text")
    @POST
    @Produces(MediaType.APPLICATION_JSON+";charset=utf-8")
    @Consumes(MediaType.APPLICATION_JSON)
    public String axiosTest(String query){
        try(Connection conn = DB.getConnection ();
            DSLContext create = DSL.using ( conn, SQLDialect.MYSQL )){
                JsonObject jsonObject=new JsonParser ().parse ( query ).getAsJsonObject ();
                String params=getJsonAsString ( jsonObject,"params" );
                Integer test=getJsonAsInt ( jsonObject,"test");
              /*  Result result=create.select (  )
                        .from ( SHOP_TEST )
                        .fetch ();
                JsonArray jsonArray=dataChangeJsonArray ( result );*/
                if (test==1){
                    throw new JerseyException("我是1号错误");
                }else if (test==2){
                    throw new JerseyException ( "我是2号错误" );
                }else if (test==3){
                    throw  new JerseyException ( "我是其他错误" );
                }
                return success ();
        }catch (Exception e){
            return error (-1, e.getMessage ());
        }
    }


    /**
     * 获取车辆的接口
     * @param query
     * @return
     */
    @Path ("get_car")
    @POST
    @Produces(MediaType.APPLICATION_JSON+";charset=utf-8")
    @Consumes(MediaType.APPLICATION_JSON)
    public String getCar(String query){
        JsonObject json=new  JsonParser ().parse ( query ).getAsJsonObject ();
        Integer parentid=getJsonAsInt ( json,"parentid" );
        Integer carid=getJsonAsInt ( json,"carid" );
        Integer type=getJsonAsInt ( json,"type" );
        //1.代表查询所有车牌 2.根据车牌获取车型 3.根据车型获取车 4.根据车id获取车详情
        JsonObject result=new JsonObject ();
        if (type==1){
            result=HttpUntil.getCarbrand ();
        }else if (type==2){
            result=HttpUntil.getCarBytype ( parentid );
        }else if (type==3){
            result=HttpUntil.getCarByparentid (parentid);
        }else if (type==4){
            result=HttpUntil.getCarBydetail ( carid );
        }
        return success (result);
    }


    /**
     * 获取天气
     * @return
     */
    @Path ("get_weather")
    @POST
    @Produces(MediaType.APPLICATION_JSON+";charset=utf-8")
    @Consumes(MediaType.APPLICATION_JSON)
    public String getWeather(){
        JsonObject result=HttpUntil.getWeather ( IpUtil.getIpAddr ( req ));
        return success (result);
    }


}
